<template>
  <el-collapse v-model="activeName">
    <!-- v-if="props.layout.props.control" -->
    <el-collapse-item name="one" v-if="props.layout.props.control">
      <template #title>
        <div class="px-[10px]">
          <span> 组件交互</span>

          <el-button
            class="ml-[10px]"
            type="primary"
            size="small"
            icon="Plus"
            color="#3a89fe"
            @click.stop="addCard"
            >新增</el-button
          >
        </div>
      </template>
      <el-empty
        :image-size="50"
        v-if="
          !states?.event?.targetComponent ||
          states?.event?.targetComponent.length == 0
        "
      ></el-empty>

      <el-scrollbar max-height="520" v-else>
        <el-card
          class="w-[94%] mx-[auto] my-[10px]"
          v-for="(item, index) in states?.event?.targetComponent"
          :key="item.targetComponentId + 1"
        >
          <template #header>
            <div class="flex justify-between items-center select-none">
              <span>关联组件</span>
              <el-icon
                class="cursor-pointer"
                color="#cb2424"
                size="18"
                @click="deleteCard(index)"
                ><Close
              /></el-icon>
            </div>
          </template>
          <el-form label-width="auto" :model="states.event" size="default">
            <div
              class="select-none mb-[10px] w-[100px] h-[30px] leading-[30px] text-center !text-[var(--active-color)] text-[14px] bg-[rgba(99,134,208,0.3)]"
            >
              选择目标组件
            </div>

            <el-form-item label="绑定事件">
              <el-select
                v-model="states.event.clickType"
                placeholder="选择要进行的操作"
                :teleported="false"
              >
                <el-option
                  v-for="item in states.event.clickTypeOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="选择组件">
              <el-select
                clearable
                v-model="states.event.targetComponent[index].targetComponentId"
                placeholder="只展示符合条件的组件"
              >
                <el-option
                  v-for="(item, i) in canChooseComp"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="查询结果">
              <Combine :tableData="states.event.mapFields" />
            </el-form-item>
            <div v-if="states.event.targetComponent[index].targetComponentId">
              <div class="flex items-center mb-[10px]">
                <div
                  class="select-none w-[120px] h-[30px] leading-[30px] text-center !text-[var(--active-color)] text-[14px]"
                >
                  关联Params参数
                </div>
                <span
                  class="text-[#ccc] ml-[30px]"
                  v-show="
                    paramsList(
                      states.event.targetComponent[index].targetComponentId
                    ).length == 0
                  "
                  >暂无数据</span
                >
              </div>
              <selectFields
                :mapFields="states.event.mapFields"
                :keyList="
                  paramsList(
                    states.event.targetComponent[index].targetComponentId
                  )
                "
                v-model:params="
                  states.event.targetComponent[index].targetComponentParams
                "
              />
              <div class="flex items-center mb-[10px]">
                <div
                  class="select-none w-[120px] h-[30px] leading-[30px] text-center !text-[var(--active-color)] text-[14px]"
                >
                  关联Datas参数
                </div>
                <span
                  class="text-[#ccc] ml-[30px]"
                  v-show="
                    dataList(
                      states.event.targetComponent[index].targetComponentId
                    ).length == 0
                  "
                  >暂无数据</span
                >
              </div>
              <selectFields
                :mapFields="states.event.mapFields"
                :keyList="
                  dataList(
                    states.event.targetComponent[index].targetComponentId
                  )
                "
                v-model:params="
                  states.event.targetComponent[index].targetComponentData
                "
              />
            </div>
          </el-form>
        </el-card>
      </el-scrollbar>
    </el-collapse-item>

    <el-collapse-item name="two">
      <template #title>
        <div class="px-[10px]">
          <span> 事件配置</span>

          <el-button
            class="ml-[10px]"
            type="primary"
            size="small"
            icon="Edit"
            color="#3a89fe"
            @click.stop="updateCode"
            >编辑</el-button
          >
        </div>
      </template>

      <div class="text-[13px] px-[10px] text-[#ccc] mt-[14px] mb-[10px]">
        点击事件
      </div>

      <el-scrollbar height="100">
        <div
          class="w-[94%] h-[100px] rounded-md mx-[auto] !bg-[#18181c] px-[4px]"
        >
          <div>{{ props.layout.eventMethods.clickMethod }}</div>
        </div>
      </el-scrollbar>
      <div class="text-[13px] px-[10px] text-[#ccc] mt-[14px] mb-[10px]">
        鼠标移入事件
      </div>
      <el-scrollbar height="100">
        <div
          class="w-[94%] h-[100px] rounded-md mx-[auto] !bg-[#18181c] px-[4px]"
        >
          <div>{{ props.layout.eventMethods.mouseenterMethod }}</div>
        </div>
      </el-scrollbar>
      <div class="text-[13px] px-[10px] text-[#ccc] mt-[14px] mb-[10px]">
        渲染前
      </div>
      <el-scrollbar height="100">
        <div
          class="w-[94%] h-[100px] rounded-md mx-[auto] !bg-[#18181c] px-[4px]"
        >
          <div>{{ props.layout.eventMethods.beforeMountMethod }}</div>
        </div>
      </el-scrollbar>
      <div class="text-[13px] px-[10px] text-[#ccc] mt-[14px] mb-[10px]">
        渲染后
      </div>
      <el-scrollbar height="100">
        <div
          class="w-[94%] h-[100px] rounded-md mx-[auto] !bg-[#18181c] px-[4px]"
        >
          <div>{{ props.layout.eventMethods.mountedMethod }}</div>
        </div>
      </el-scrollbar>
    </el-collapse-item>
  </el-collapse>
  <EventConfgDialog
    ref="dialog"
    v-if="showDialog"
    :layout="props.layout"
    @updateData="updateEventData"
    @close="closeEdit"
  />
</template>

<script setup lang="ts">
import { computed, ref, inject, nextTick } from "vue";
import EventConfgDialog from "@renderer/components/eventConfgDialog/index.vue";
import Combine from "./combineComp.vue";
import selectFields from "./selectFields.vue";
import { watch } from "fs";
type PropsType = {
  layout: any;
  layoutList: any;
};
let dialog = ref(null);
let activeName = ref(["two"]);
let props = withDefaults(defineProps<PropsType>(), {
  layout: {},
  layoutList: [],
});
let emit = defineEmits(["update:layout"]);
let states = computed({
  get() {
    return props.layout.props;
  },
  set() {
    emit("update:layout");
  },
});

let canChooseComp = computed(() => {
  let arr = props.layoutList
    .filter((item: any) => item.canReq && item.id !== props.layout.id)
    .map((de) => ({
      label: de.componentLabelName,
      value: de.id,
    }));

  arr.forEach((it) => {
    let flag = states.value.event.targetComponent
      .map((it) => it.targetComponentId)
      .includes(it.value);
    if (flag) {
      it.disabled = true;
    }
  });
  // console.log("arr", arr);
  return arr;
});

let paramsList = computed(() => {
  return (id) => {
    let item = props.layoutList.find((it) => it.id == id);
    if (item) {
      return Object.keys(item.request.params) || [];
    }
  };
});
let dataList = computed(() => {
  return (id) => {
    let item = props.layoutList.find((it) => it.id == id);
    if (item) {
      return Object.keys(item.request.data) || [];
    }
  };
});

let deleteCard = (index: number) => {
  states.value.event.targetComponent.splice(index, 1);
};
let addCard = () => {
  console.log("states.value.id", props.layout);
  states.value.event.targetComponent.push({
    currentId: props.layout.id,
    targetComponentId: "",
    targetComponentParams: {},
    targetComponentData: {},
  });
};
let showDialog = ref(false);
const { updateCanShortKey } = inject("canShortKey") as any;
let updateCode = () => {
  updateCanShortKey(false);
  showDialog.value = true;
  nextTick(() => {
    dialog.value.dialogVisible = true;
  });
};

let updateEventData = (value: any) => {
  let layout = JSON.parse(JSON.stringify(props.layout));
  emit("update:layout", {
    ...layout,
    eventMethods: {
      ...value,
      filterMethod: layout.eventMethods.filterMethod,
      ifFilter: layout.eventMethods.ifFilter,
    },
  });
  closeEdit();
};

let closeEdit = () => {
  updateCanShortKey(true);
  showDialog.value = false;
};
</script>

<style scoped lang="scss">
:deep(.el-table tr) {
  background-color: #232324 !important;
}
:deep(.el-table th.el-table__cell) {
  background: #39393e !important;
}

:deep(.el-table th.el-table__cell.is-leaf) {
  border-bottom-color: #646567 !important;
}
:deep(.el-table td.el-table__cell) {
  border-bottom-color: #646567 !important;
  font-size: 11px !important;
  user-select: none;
}
:deep(.el-table__inner-wrapper:before) {
  height: 0 !important;
}

:deep(
    .el-table--enable-row-hover .el-table__body tr:hover > td.el-table__cell
  ) {
  background-color: #232324 !important;
}
</style>
